//Component Object
import router from '@src/router'
import {
  applyForInvoice,
  reloadInvoice
} from "@src/api/invoice";
import {
  storeBindingsBehavior
} from "mobx-miniprogram-bindings";
import base from "@src/store/base";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  properties: {
    popup: {
      type: Boolean,
      value: false,
      observer: function (val) {
        if (val) {
          const that = this
          wx.getSystemInfo({
            success: function (res) {
              that.setData({
                windowWidth: res.windowWidth,
                windowHeight: res.windowHeight,
                showCover: val
              })
            },
          })
          setTimeout(() => {
            this.setData({
              showContent: val
            })
          }, 200)
        } else {
          this.setData({
            showContent: val
          })
          setTimeout(() => {
            this.setData({
              showCover: val
            })
          }, 200)
        }
      }
    },
    formData: {
      type: Object,
    }

  },
  options: {
    multipleSlots: true
  },
  data: {
    showCover: false, // 整个dialog dom节点
    showContent: false,
    isShow: false, // 回调弹窗
    titleTypeMap: ['企业', '个人'],
    isSucceed: false,
    errRemark: null,
    windowWidth: '',
    windowHeight: '',
    loading: false
  },
  methods: {
    close() {
      this.triggerEvent('onPopup', false);
    },
    cancel() {
      this.triggerEvent('onPopup', false);
    },
    handerOk() {
      if (this.data.loading) return
      const params = Object.assign({}, this.data.formData)
      for (const key in params) {
        if (params[key] === null || params[key] === '') {
          delete params[key]
        }
      }
      this.setData({
        loading: true
      })
      wx.showLoading({
        title: '开票中...',
      })
      if (this.data.formData.id) {
        // 历史开票
        reloadInvoice(params).then(res => {
          this.setData({
            showContent: false,
            isSucceed: true
          })
          setTimeout(() => {
            this.setData({
              isShow: true
            })
          }, 200)
        }).catch(err => {
          this.setData({
            showContent: false,
            isSucceed: false,
          })
          this.triggerEvent('onPopup', false);
        }).finally(() => {
          setTimeout(() => { // 目的防止用户过多点击所有给一个2秒的延迟
            this.setData({
              loading: false
            })
            wx.hideLoading()
          }, 2000)
        })
      } else {
        // 新开票
        applyForInvoice(params).then(res => {
          this.setData({
            showContent: false,
            isSucceed: true
          })
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 2000,
            success: (() => {
              setTimeout(() => {
                this.openFaPiaoOkFunc()
              }, 2000)
            })
          })
        }).catch(err => {
          this.setData({
            showContent: false,
            isSucceed: false,
          })
          this.triggerEvent('onPopup', false);
        }).finally(() => {
          setTimeout(() => {
            this.setData({
              loading: false
            })
            wx.hideLoading()
          }, 2000)
        })
      }

    },
    // 回调弹窗传参
    onOver(e) {
      this.setData({
        isShow: e.detail
      })
      this.triggerEvent('onPopup', false);
    },
    //传开票成功标识（为了开票成功，返回上一页后，刷新列表）
    openFaPiaoOkFunc(){
      // router.back()传值
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      prevPage.setData({
        openFapiaoStatus: 'success',
      })
      router.back()

      // 接收页通过以下方式接收：
      // onShow(){
      //     const pages = getCurrentPages()
      //     const currPage = pages[pages.length - 1]
      //     console.log('接收门店列表的数据 currPage', currPage.data.storeData)
      // },
    }
  }
});